package com.jilujia.framework.domain;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

/**
 * 
 */
public class SearchCriteria implements Serializable {
	// TODO 需要限制pagesize最大值或者限制不能人为修改pagesize最大值
	/**
	 * 
	 */
	private static final long serialVersionUID = -3645634931991251364L;

	private final static int DEFAULT_PAGESIZE = 50;

	private final static int DEFAULT_PAGE = 1;

	private final static String DEFAULT_SORT = "newest";

	private final static String POP_SORT = "pop";

	/**
	 * The maximum page size of the Hotel result list
	 */
	private int pageSize = DEFAULT_PAGESIZE;

	/**
	 * The current page of the Hotel result list.
	 */
	private int page = DEFAULT_PAGE;

	private String sort = DEFAULT_SORT;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if (pageSize <= 0) {
			pageSize = DEFAULT_PAGESIZE;
		}
		this.pageSize = pageSize;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		if (page <= 0) {
			page = DEFAULT_PAGE;
		}

		this.page = page;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		if (StringUtils.isBlank(this.sort)) {
			sort = DEFAULT_SORT;
		}

		this.sort = sort;
	}

	public PageRequest getPageRequest() {
		PageRequest pageRequest = new PageRequest(this.getPage() - 1, this.getPageSize());
		return pageRequest;
	}

	public PageRequest getSortedPageRequest() {
		// add sort
		Sort sort = null;

		if (DEFAULT_SORT.equals(this.getSort())) {
			sort = new Sort(Direction.DESC, "modifyDate");
		} else if (POP_SORT.equals(this.getSort())) {
			sort = new Sort(Direction.DESC, "pop");
		} else {
			sort = new Sort(Direction.DESC, "modifyDate");
		}

		PageRequest pageRequest = new PageRequest(this.getPage() - 1, this.getPageSize(), sort);
		return pageRequest;
	}

	public final static PageRequest getTop10PageRequest() {
		PageRequest pageRequest = new PageRequest(0, 10);
		return pageRequest;
	}

}